<?php
class Lead_EDO_Datatype_Field_Relation implements Lead_EDO_Datatype_Field_Interface
{
    private $_owner;
    private $_config;
    
    private $_messages;
        
    public function __construct($owner, $config)
    {
        $this->_owner = $owner;
        
        $this->_config = $config;    
    }
    
    public function getOwner()
    {
        return $this->_owner;
    }
    
    
    public function getName()
    {
        return $this->_config['name'];
    }
    
    
    
    public function isNotNull()
    {
        $class = 'Lead_EDO_Datatype_Field_Relation_' . $this->getType();
        
        Zend_Loader::loadClass($class);
        
        return call_user_func(array($class, 'isNotNull'), $this);
        
    }
    
    
    
    public function getForeignKey()
    {
        return (isset($this->_config['foreign_key'])) ? $this->_config['foreign_key'] : strtolower($this->getRelatedClass()) . '_id';  
    }
    
    
    public function getRelatedClass()
    {
        return $this->_config['class_name'];
    }
    
    
    
    public function getType()
    {
        return $this->_config['type'];
    }
        
    
    public function getConfig($key = null)
    {
        if(!$key)
            return $this->_config;
        
        return (key_exists($key, $this->_config)) ? $this->_config[$key] : false;
    }
    
    
    
    public function validate($value)
    {
        return Lead_EDO_datatype_Validator::validate($this, $value);
    }
        
    public function setValue($value)
    {
        if(!$this->validate($value)) {
            return false;
        }
        
        $this->_value = $value;
        return true;
        
        
    }
    
    public function getValue()
    {
        $class = 'Lead_EDO_Datatype_Field_Relation_' . $this->getType();
        Zend_Loader::loadClass($class);
        return call_user_func(array($class, 'getValue'), $this);        
     
  
    }
    
    
    public function setValidationMessages($messages = array())
    {
        $this->_messages = $messages;
    }
    
    public function getValidationMessages()
    {
        return $this->_messages;
    }
    
    
}